package com.mycjj.android.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.mycjj.android.MyCjjApplication;
import com.mycjj.android.R;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public abstract class TAdapter<T> extends android.widget.BaseAdapter {
	protected List<T> mList;
	protected Context mContext;
	protected ListView mListView;
	protected MyCjjApplication app;
	protected final static String APP_URL = ProjectHttpRequestInterface.APP_URL;
	protected DisplayImageOptions options;

	protected ImageLoader imageLoader1;
	
	protected SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	protected ImageLoadingListener animateFirstListener;
	public TAdapter(Context mContext) {
		this.mContext = mContext;
		app = MyCjjApplication.getInstance();
		imageLoader1 = ImageLoader.getInstance();
		
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_empty)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
//				.displayer(new RoundedBitmapDisplayer(20))
				.build();
		animateFirstListener = new AnimateFirstDisplayListener();
	}
	
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

	@Override
	public int getCount() {
		if (mList != null)
			return mList.size();
		else
			return 0;
	}

	@Override
	public Object getItem(int position) {
		return mList == null ? null : mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	public void setList(T[] list) {
		if (list == null || list.length == 0) {
			return;
		}
		List<T> arrayList = new ArrayList<T>(list.length);
		for (T t : list) {
			arrayList.add(t);
		}
		setList(arrayList);
	}

	public void setList(List<T> list) {
		if (list == null || list.isEmpty()) {
			return;
		}
		this.mList = new ArrayList<T>();
		this.mList.addAll(list);
		notifyDataSetChanged();
	}

	public void notifyChangeEmpty() {
		this.mList = new ArrayList<T>();
		notifyDataSetChanged();
	}

	public ListView getListView() {
		return mListView;
	}

	public void setListView(ListView listView) {
		mListView = listView;
	}
}
